/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package contabilidad;

import conexion.CConexion;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import validaciones.CLogin;

/**
 *
 * @author SISTEMAS
 */
public class CCo021 extends CConexion {

    CLogin login = null;
    String compania = null;

    public CCo021() {
        super();
        login = new CLogin();
        compania = login.getCompaniaActual();
    }

    public ResultSet BuscarCuentas(String niv1, String niv2, String niv3, String niv4, String niv5, String desc, String ctac) throws SQLException {
        ResultSet rsCuentas = null;
        String sql = "select co021niv1,co021niv2,co021niv3,co021niv4,co021niv5,co021desc,co021ctac,co021cmay from co021 where co021nocia = '" + compania + "'";
        if (!niv1.isEmpty()) {
            sql += " and co021niv1 = '" + niv1 + "'";
        }
        if (!niv2.isEmpty()) {
            sql += " and co021niv2 = '" + niv2 + "'";
        }
        if (!niv3.isEmpty()) {
            sql += " and co021niv3 = '" + niv3 + "'";
        }
        if (!niv4.isEmpty()) {
            sql += " and co021niv4 = '" + niv4 + "'";
        }
        if (!niv5.isEmpty()) {
            sql += " and co021niv5 = '" + niv5 + "'";
        }
        if (!desc.isEmpty()) {
            sql += " and co021desc like '%" + desc + "%'";
        }
        if (!ctac.isEmpty()) {
            sql += " and co021ctac like '%" + ctac + "%'";
        }
        sql += " order by co021ctac";
        Statement stncCuentas = this.getConexion().createStatement();
        rsCuentas = stncCuentas.executeQuery(sql);
        return rsCuentas;
    }

    public ResultSet buscarCuenta(String cuentaUnida) throws SQLException {
        String sqlCuenta = "select co021niv1,co021niv2,co021niv3,co021niv4,co021niv5,co021desc from co021 where co021nocia = '" + compania + "' and co021ctac = '" + cuentaUnida + "'";
        Statement stncCuenta = this.getConexion().createStatement();
        ResultSet rsCuenta = stncCuenta.executeQuery(sqlCuenta);
        return rsCuenta;
    }

    public ResultSet buscarCuentaPorNiveles(String niv1, String niv2, String niv3, String niv4, String niv5) throws SQLException {
        ResultSet rsCuentas = null;
        String sql = "select co021niv1,co021niv2,co021niv3,co021niv4,co021niv5,co021desc,co021ctac,co021cmay from co021 where co021nocia = '" + compania + "' and co021niv1 = '" + niv1 + "' and co021niv2 = '" + niv2 + "' and co021niv3 = '" + niv3 + "' and co021niv4 = '" + niv4 + "' and co021niv5 = '" + niv5 + "'";
        Statement stncCuentas = this.getConexion().createStatement();
        rsCuentas = stncCuentas.executeQuery(sql);
        return rsCuentas;
    }

    public String descripcionCuenta(String niv1, String niv2, String niv3, String niv4, String niv5) throws SQLException {
        String sqlDescripcion = "select co021desc from co021 where co021nocia='" + compania + "' and co021niv1 = '" + niv1 + "' and co021niv2 = '" + niv2 + "' and co021niv3 = '" + niv3 + "' and co021niv4 = '" + niv4 + "' and co021niv5 = '" + niv5 + "' ";
        Statement stncDescripcion = this.getConexion().createStatement();
        ResultSet rsDescripcion = stncDescripcion.executeQuery(sqlDescripcion);
        rsDescripcion.next();
        String descripcion = rsDescripcion.getString(1);
        rsDescripcion.close();
        stncDescripcion.close();
        return descripcion;
    }

    public boolean ctacmov(String niv1, String niv2, String niv3, String niv4, String niv5) throws SQLException {
        String sqlCo000 = " SELECT co021inmo FROM co021 where co021nocia ='" + compania + "' and co021niv1='" + niv1 + "' and co021niv2='" + niv2 + "' and co021niv3='" + niv3 + "' and co021niv4='" + niv4 + "' and co021niv5='" + niv5 + "'";
        System.out.println(sqlCo000);
        Statement stnc = this.getConexion().createStatement();
        ResultSet rs;
        rs = stnc.executeQuery(sqlCo000);
        rs.next();
        if (rs.getString(1).equalsIgnoreCase("N")) {
            return false;
        } else {
            return true;
        }
    }

    public String[] tipoComp() throws SQLException {
        String[] A;
        Statement stnc = this.getConexion().createStatement();
        ResultSet rs;
        rs = stnc.executeQuery("SELECT co012cdco from co012 where co012nocia = '" + login.getCompaniaActual() + "'");
        int i = 0;
        while (rs.next()) {
            i = rs.getRow();
        }
        rs.beforeFirst();
        A = new String[i];
        i = 0;
        while (rs.next()) {
            A[i] = rs.getString(1);
            i++;
        }
        return A;
    }

    public String nombCuenta(String n1, String n2, String n3, String n4, String n5) throws SQLException {
        String sqlCo000 = " SELECT co021desc FROM co021 where co021niv1 ='" + n1 + "' and co021niv2 ='" + n2 + "' and co021niv3 ='" + n3 + "' and co021niv4 ='" + n4 + "' and co021niv5 ='" + n5 + "'";
        Statement stnc = this.getConexion().createStatement();
        //stnc = this.getConexion().createStatement();
        ResultSet rs1 = stnc.executeQuery(sqlCo000);
        rs1.next();
        return rs1.getString(1);
    }

    public String descripcion(String niv1, String niv2, String niv3, String niv4, String niv5) throws SQLException, IOException {
        Statement stnc = this.getConexion().createStatement();
        String sql = "select co021desc from co021 where co021nocia='" + login.getCompaniaActual()
                + "' and co021niv1 = '" + niv1 + "' and co021niv2 = '" + niv2 + "' and co021niv3 = '" + niv3 + "' and co021niv4 = '" + niv4 + "' and co021niv5 = '" + niv5 + "' ";
        System.out.println(sql);
        ResultSet ra = stnc.executeQuery(sql);
        ra.next();
        return ra.getString(1);
    }

    public boolean ctaccco(String niv1, String niv2, String niv3, String niv4, String niv5) throws SQLException {
        String sqlCo000 = " SELECT co021incc FROM co021 where co021nocia ='" + login.getCompaniaActual() + "' and co021niv1='" + niv1 + "' and co021niv2='" + niv2 + "' and co021niv3='" + niv3 + "' and co021niv4='" + niv4 + "' and co021niv5='" + niv5 + "'";
        Statement stnc = this.getConexion().createStatement();
        ResultSet rs;
        rs = stnc.executeQuery(sqlCo000);
        rs.next();
        if (rs.getString(1).equalsIgnoreCase("N")) {
            return false;
        } else {
            return true;
        }
    }
}
